package com.ultrapower.ctool.xn_app;

import android.content.Intent;
import android.os.Bundle;
import android.os.Handler;
import android.os.Message;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import android.widget.Toast;

import com.google.gson.Gson;
import com.ultrapower.ctool.ConfUtils;
import com.ultrapower.ctool.core.CustomApplication;
import com.ultrapower.ctool.http.BaseActivity;

import java.util.HashMap;
import java.util.Map;

import okhttp3.FormBody;
import okhttp3.OkHttpClient;
import okhttp3.Request;
import okhttp3.RequestBody;
import okhttp3.Response;

public class UserCenterActivity extends BaseActivity implements View.OnClickListener {

    private Button changeUserInfoButton;
    private Button infoBackButton;
    private EditText nickname;
    private EditText email;
    private EditText mphone;
    private EditText xid;
    private EditText dtype;
    private EditText zjtype;
    private EditText zjbh;
    private EditText xb;
    private EditText post;
    private EditText xm;
    private EditText rid;
    private EditText pstatus;
    private CustomApplication application;
    //private OkHttpClient client = new OkHttpClient();
    private Gson gson = new Gson();
    private Map<String, EditText> textMap = new HashMap<String, EditText>();
    private Handler handler = new Handler() {
        @Override
        public void handleMessage(Message msg) {
            switch (msg.what) {
                case 1:
                    Toast.makeText(getApplicationContext(), "用户名详情成功", Toast.LENGTH_SHORT).show();

                    finish();
                    break;
                case 0:
                    Toast.makeText(getApplicationContext(), "没获得相关数据", Toast.LENGTH_SHORT).show();
                    break;
            }
        }
    };
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_user_center);
        init();
    }

    private void init() {
        changeUserInfoButton = (Button) findViewById(R.id.change_userInfo);
        infoBackButton = (Button) findViewById(R.id.info_back);
        nickname = (EditText) findViewById(R.id.nickname);
        textMap.put("nickname", nickname);
        email = (EditText) findViewById(R.id.email);
        textMap.put("email", email);
        mphone = (EditText) findViewById(R.id.mphone);
        textMap.put("mphone", mphone);
        xid = (EditText) findViewById(R.id.xid);
        textMap.put("xid", xid);
        dtype = (EditText) findViewById(R.id.dtype);
        textMap.put("dtype", dtype);
        zjtype = (EditText) findViewById(R.id.zjtype);
        textMap.put("zjtype", zjtype);
        zjbh = (EditText) findViewById(R.id.zjbh);
        textMap.put("zjbh", zjbh);
        xb = (EditText) findViewById(R.id.xb);
        textMap.put("xb", xb);
        post = (EditText) findViewById(R.id.post);
        textMap.put("post", post);
        xm = (EditText) findViewById(R.id.xm);
        textMap.put("xm", xm);
        rid = (EditText) findViewById(R.id.rid);
        textMap.put("rid", rid);
        pstatus = (EditText) findViewById(R.id.pstatus);
        textMap.put("pstatus", pstatus);
        application = (CustomApplication) getApplication();

        new Thread(new Runnable() {
            @Override
            public void run() {
                try {
                    OkHttpClient client = new OkHttpClient();
                    RequestBody requestBody = new FormBody.Builder()
                            .add("userId", String.valueOf(application.getLoginUser().getLogin_id()))
                            .build();
                    Request request = new Request.Builder().url(ConfUtils.SERVICE_URL + "user/detail/c_pt_user").post(requestBody).build();
                    Response response = client.newCall(request).execute();
                    if (response.isSuccessful()) {//成功
                        String result = response.body().string();
                        System.out.println("result:"+result);
                        final Map<String, String> resultMap = gson.fromJson(result, Map.class);
                        //System.out.println(resultMap);
                        runOnUiThread(new Runnable() {
                            @Override
                            public void run() {
                                for (Map.Entry<String, String> entry : resultMap.entrySet()) {
                                   System.out.println(entry.getKey() + "---------" + entry.getValue());
                                    if (textMap.get(entry.getKey()) != null) {
                                        textMap.get(entry.getKey()).setText(entry.getValue());
                                    }
                                }
                            }
                        });
                    }else {
                        Message message = new Message();
                        message.what = 0;
                        handler.sendMessage(message);
                    }
                } catch (Exception e) {
                    Message message = new Message();
                    message.what = 0;
                    handler.sendMessage(message);
                    e.printStackTrace();
                }
            }
        }).start();

        changeUserInfoButton.setOnClickListener(this);
        infoBackButton.setOnClickListener(this);
    }

    @Override
    public void onClick(View v) {
        switch (v.getId()) {
            case R.id.change_userInfo:
                Intent intent1 = new Intent(UserCenterActivity.this, ChangeUserInfoActivity.class);
                startActivity(intent1);
                break;
            case R.id.info_back:
                finish();
                break;
        }
    }

}
